package com.vr.xxaiagent.rag;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document;
import org.springframework.ai.reader.markdown.MarkdownDocumentReader;
import org.springframework.ai.reader.markdown.config.MarkdownDocumentReaderConfig;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 文档加载器
 *
 * @author hzh
 * @date 2025/06/04
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class LoveAppDocumentLoader {
    /**
     * 资源解析器
     */
    private final ResourcePatternResolver resourcePatternResolver;

    /**
     * 加载 Markdown,根据规则分割成多个文档切片(包含了收集、分割)
     *
     * @return {@code List<Document> }
     */
    public List<Document> loadMarkdowns() {
        ArrayList<Document> allDocuments = new ArrayList<>();

        try {
            Resource[] resources = resourcePatternResolver.getResources("classpath*:document/*.md");
            for (Resource resource : resources) {
                String filename = resource.getFilename();
                MarkdownDocumentReaderConfig config = MarkdownDocumentReaderConfig.builder()
                        // 将 Markdown 中的水平线（---）转换为独立文档(这里相当于，etl里的t)
                        .withHorizontalRuleCreateDocument(true)
                        // 不包含引用块内容
                        .withIncludeBlockquote(false)
                        // 不包含代码块内容
                        .withIncludeCodeBlock(false)
                        // 添加文件名元数据，用于后续检索时的过滤条件/
                        .withAdditionalMetadata("filename", filename)
                        .build();
                MarkdownDocumentReader reader = new MarkdownDocumentReader(resource, config);
                // 一个markdown文件会被分割多个文档
                allDocuments.addAll(reader.get());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return allDocuments;
    }

}
